<?php
/**************************************************/
## Class Name - Model_Users (Contains all the functions related user and user profile)
## Created By - Techmodi (19/08/2011)
/**************************************************/

class Model_Users extends Database 
{	
	## Constructor
	function Model_Users() {
		$this->user = USERS;
		$this->userjobpost = POSTJOB;
		$this->trade_cat = TRADE_CATEGORIES;
		$this->userviewjob = USERVIEWJOB;
		$this->usermembership = USERMEMBERSHIP;
		$this->usermembershipdetail = USERMEMBERSHIPDETAIL;
		$this->reportabuse = REPORTABUSE;
		$this->reviews = REVIEWS;
		$this->user_workphoto = WORKPHOTO;
		$this->workarea 	= WORKAREA;	
		$this->Database();
	}	
	
	## Check admin user login
	function getUserValueByDetailsUsernameAndPassword($username, $password) {
		$fields=array();	
		$tables=array($this->user);
		$where=array("(username='".$username."'  AND  password='".md5($password)."') ");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	## Check tradesman user login
	function getUserValueByDetailsUsernameAndPasswordforTradesman($username, $password) {
		$fields=array();	
		$tables=array($this->user);
		$where=array("(username='".$username."'  AND  password='".md5($password)."') and user_type='4'");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	## Check homeowner user login
	function getUserValueByDetailsUsernameAndPasswordforHome($username, $password) {
		$fields=array();	
		$tables=array($this->user);
		$where=array("(username='".$username."'  AND  password='".md5($password)."') and user_type='3'");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	
	## Get user details by userid
	function getUserDetailsByUserId($id) {
		$fields=array();	
		$tables=array($this->user);
		$where=array("id=".$id);		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	
	## Get user details by Company name
	function getUserDetailsByCompanyName($companyname) {
		$fields=array();	
		$tables=array($this->user);
		$where=array("companyname='".strtolower($companyname)."'");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	## Get user Work Photo details by photo id
	function getUserPhotoDetailsByPhotoId($id) {
		$fields=array();	
		$tables=array($this->user_workphoto);
		$where=array("photo_id=".$id);		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	## Get user details by userid
	function getAllUsers($search,$col1='',$col2='',$col3='',$limit='',$offset='') {
		$fields=array();	
		$tables=array($this->user);
		$where = array(" user_type != '1' AND user_type != '2' ");
		if($search != '') {
			//$where[] = "concat(first_name,' ',last_name) like '%".$search."%'";
			$where[] = "(concat(first_name,' ',last_name) LIKE '%".$search."%' OR email LIKE '%".$search."%' )";
		}
		if($col1 =='0')
		{
			$order = array("first_name ASC");
		}
		if($col2 =='0')
		{
			$order = array("email ASC");
		}
		if($col3=='0')
		{
			$order = array("registered_date ASC");
		}
		if($col1 =='1')
		{
			$order = array("first_name DESC");
		}
		if($col2 =='1')
		{
			$order = array("email DESC");
		}
		if($col3=='1')
		{
			$order = array("registered_date DESC");
		}
			
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
	
	## Get all homeowner user
	function getAllHomeOwnerUsers($search,$col1='',$col2='',$col3='',$limit='',$offset='') {
		$fields=array();	
		$tables=array($this->user);
		$where = array(" user_type = '3' ");
		if($search != '') 
		{
			//$where[] = "concat(first_name,' ',last_name) like '%".$search."%'";
			$where[] = "(concat(first_name,' ',last_name) LIKE '%".$search."%' OR email LIKE '%".$search."%' )";
		}
		if($col1 =='0')
		{
			$order = array("first_name ASC");
		}
		elseif($col2 =='0')
		{
			$order = array("email ASC");
		}
		elseif($col3=='0')
		{
			$order = array("registered_date ASC");
		}
		elseif($col1 =='1')
		{
			$order = array("first_name DESC");
		}
		elseif($col2 =='1')
		{
			$order = array("email DESC");
		}
		elseif($col3=='1')
		{
			$order = array("registered_date DESC");
		}
		else
		{
			$order =array("id DESC");
		}
			
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
	
	## Get all tradesman user
	function getAllTradesmanUsers($search,$col1='',$col2='',$col3='',$limit='',$offset='') {
		$fields=array();	
		$tables=array($this->user);
		$where = array(" user_type = '4' ");
		if($search != '') 
		{
			//$where[] = "concat(first_name,' ',last_name) like '%".$search."%'";
			$where[] = "(concat(first_name,' ',last_name) LIKE '%".$search."%' OR email LIKE '%".$search."%')";
		}
		if($col1 =='0')
		{
			$order = array("first_name ASC");
		}
		
		elseif($col1 =='1')
		{
			$order = array("first_name DESC");
		}
		
		else
			$order = array("id DESC");
			
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
	
	## check email address is present
	function checkUserEmailPresent($email) {
		$fields=array("id, email");	
		$tables=array($this->user);
		$where=array("email ='".$email."'");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
		## check Admin email address is present
	function checkAdminEmailPresent($email) {
		$fields=array("id, email");	
		$tables=array($this->user);
		$where=array("email ='".$email."'","user_type IN (1, 2)");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	## Add user in database
	function addUserByValue($Array) {
		$this->InsertData( $this->user , $Array );		
		$insertId = mysql_insert_id();
		return $insertId;
	}	
	
	## Add user work photo in database
	function addUserWorkPhotoByValue($Array) {
		$this->InsertData( $this->user_workphoto , $Array );		
		$insertId = mysql_insert_id();
		return $insertId;
	}	
	
	## Get user details 
	function getAllWorkPhotoByUserID($userid,$searchtxt='',$limit='',$offset='') {
		$fields=array();	
		$tables=array($this->user_workphoto);
		$where = array(" user_id=$userid");
		if($searchtxt != '') 
		{		
			$where[] = "(title LIKE '%".$searchtxt."%')";
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("photo_id DESC"), $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}
	// Delete work photo
	function deleteWorkPhotoById($id)
	{
		$this->DeleteData($this->user_workphoto,"photo_id",$id);
	}
	
	## Add user job post in database
	function addUserPostJobByValue($Array) {
		$this->InsertData( $this->userjobpost , $Array );		
		$insertId = mysql_insert_id();
		return $insertId;
	}	
	
	## Edit user by userid
	function editUserValueById($array, $Id){
		$this->UpdateData($this->user,$array,"id",$Id,0);
	}
	## Edit work photo by photoid
	function editUserWorkPhotoByPhotoID($array, $Id){
		$this->UpdateData($this->user_workphoto,$array,"photo_id",$Id,0);
	}

	## Delete user by userid
	function deleteUserValueById($id){
		$this->DeleteData($this->user,"id",$id);
		$this->DeleteData($this->userjobpost,"user_id",$id);
	}
	## fetch last 5 registered user
	function getLasrFiveRegistedUser() {
		$fields=array();	
		$tables=array($this->user);
		$where=array("user_type!='1' and user_type!='2' ");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("registered_date DESC"), $group=array(),$limit = "5",0,0); 
		$result= $this->FetchAll($result1); 
		return $result;	
	}
	
	## Update user status with multiple ids
	function updateUserStatus($ids, $status) {
		$sql = "UPDATE ".$this->user." SET user_status='".$status."' WHERE id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Delete faq category with multiple ids
	function deleteUsers($ids) {
		$sql = "DELETE FROM ".$this->user." WHERE id IN ('".$ids."')";
		$result1= $this->ExecuteQuery($sql);	 
		
		$sql2 = "DELETE FROM ".$this->userjobpost." WHERE user_id IN (".$ids.")";
		$result2= $this->ExecuteQuery($sql2);	 
	}
	
	## Get user First name And last Name by userid
	function getUserNameByUserId($id) {
		$fields=array('first_name','last_name');	
		$tables=array($this->user);
		$where=array("id=".$id);		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result['first_name'].' '.$result['last_name'];		
	}
	
	## Get user details 
	function getAllActiveUsers() {
		$fields=array();	
		$tables=array($this->user);
		$where = array(" user_type != '1' AND user_type != '2' AND user_status = '1'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("first_name ASC"), $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}
	
	## Get user details 
	function getAllActiveUsersWithAdmin() {
		$fields=array();	
		$tables=array($this->user);
		$where = array(" user_status = '1'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("first_name ASC"), $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}

	
	## get user Details by MD5 User Id
	function getUserValueByEncryptedId($userId)
	{
		$fields=array();
		$tables=array($this->user);
		$where=array("md5(id)='".$userId."'");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	
		## Edit user by MD5 userid
	function editUserValueByMd5ofId($array, $Id){
		$this->UpdateData($this->user,$array,"md5(id)",$Id,0);
	}
	
		// Global check username exist or not	
	function chkUserNameExist($email, $id='') {
		$fields=array();
		$tables=array($this->user);
		$where=array("email='".$email."'");
		if($id != '') {
			$where[] = " id!=".$id;	
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	
	## Get all homeowner user post job
	function getAllHomeOwnerUsersPostJobs($userid,$search,$jobtypeid='',$col1='',$limit='',$offset='') {
		$fields=array("$this->userjobpost.*,$this->trade_cat.trade_category");	
		$tables=array($this->userjobpost,$this->trade_cat);
		$where = array("$this->userjobpost.trade_id = $this->trade_cat.id and $this->userjobpost.user_id=".$userid);
		if($search != '') 
		{
			//$where[] = "concat(first_name,' ',last_name) like '%".$search."%'";
			$where[] = "($this->trade_cat.trade_category LIKE '%".$search."%' )";
		}		
		if($jobtypeid!='')
			$where[] = "($this->userjobpost.jobtype_id ='".$jobtypeid."' )";
			
		if($col1 =='0')
		{
			$order = array("$this->trade_cat.trade_category ASC");
		}
		if($col1 =='1')
		{
			$order = array("$this->trade_cat.trade_category DESC");
		}
	
			
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}
	
	## Get post job detail by id
	function getJobDetailsById($id) {
		$fields=array("$this->user.first_name,$this->user.last_name,$this->user.phone_number,$this->user.email,$this->userjobpost.*,$this->trade_cat.trade_category");	
		$tables=array($this->user,$this->userjobpost,$this->trade_cat);
		$where=array("$this->user.id = $this->userjobpost.user_id and  $this->userjobpost.trade_id = $this->trade_cat.id and postjob_id=".$id);		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	## Edit post job by jobid
	function editJobPostValueByJOBId($array, $Id){
		$this->UpdateData($this->userjobpost,$array,"postjob_id",$Id,0);
	}

	## Get post job count by  userid
	function getAllHomeOwnerUsersPostJobsCount($userid) {
		$fields=array("count(postjob_id) jobcnt");	
		$tables=array($this->userjobpost);
		$where=array("user_id=".$userid);		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result['jobcnt'];		
	}
	
	## Get Buy Lead count by  userid
	function getAllTradeManUsersBuyLeadsCount ($userid) {
		$fields=array("count(jobid) jobcnt");	
		$tables=array("$this->userviewjob uj,$this->userjobpost jp");
		$where=array("userid=".$userid." and uj.jobid=jp.postjob_id");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result['jobcnt'];		
	}
	## Get all homeowner user postt job
	function getAllHomeOwnerUsers_POSTJOBS($search,$col1='',$limit='',$offset='') {
		$fields=array();	
		$tables=array($this->user);
		$where = array(" user_type = '3' ");
		if($search != '') 
		{
			//$where[] = "concat(first_name,' ',last_name) like '%".$search."%'";
			$where[] = "(concat(first_name,' ',last_name) LIKE '%".$search."%' )";
		}
		if($col1 =='0')
		{
			$order = array("first_name ASC");
		}
		if($col1 =='1')
		{
			$order = array("first_name DESC");
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
	
	## Get all Tradesman user
	function getAllTradesManUsers_BUYLEADS($search,$col1='',$limit='',$offset='') {
		$fields=array();	
		$tables=array($this->user);
		$where = array(" user_type = '4' ");
		if($search != '') 
		{
			//$where[] = "concat(first_name,' ',last_name) like '%".$search."%'";
			$where[] = "(concat(first_name,' ',last_name) LIKE '%".$search."%' )";
		}
		if($col1 =='0')
		{
			$order = array("first_name ASC");
		}
		if($col1 =='1')
		{
			$order = array("first_name DESC");
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order , $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
	
	## Delete postjob by postid
	function deleteJobPostValueByJOBId($id){
		$this->DeleteData($this->userjobpost,"postjob_id",$id);
	}

	## Delete postjob with multiple jobids
	function deleteMultiplePostJobs($ids) {
		$sql = "DELETE FROM ".$this->userjobpost." WHERE postjob_id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	## Update postjob with multiple jobids
	function updateMultiplePostJobstatus($ids, $status) {
		$sql = "UPDATE ".$this->userjobpost." SET status='".$status."' WHERE postjob_id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}
	
	// Global check email exist or not	
	function chkUserEmailExist($email, $id='') {
		$fields=array('email');
		$tables=array($this->user);
		$where=array("email='".$email."'","user_type!=1","user_type!=2");
		if($id != '') {
			$where[] = " id!=".$id;	
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1);
		return $result;		
	}
	
	## Get user details by postal code and job type
	function getalluserworkinpostalcode($postalcode,$jobtypeid,$tradeid) {
		$fields=array("email,first_name,last_name,id");	
		$tables=array($this->user);
		$where = array("user_type = '4' AND user_status = '1' AND workarea ='".$postalcode."' 
										AND (jobtype LIKE '".$jobtypeid."' OR jobtype LIKE '%,".$jobtypeid."' OR jobtype LIKE '%,".$jobtypeid.",%' OR jobtype LIKE '%".$jobtypeid.",%' ) 
										AND (tradeid LIKE '".$tradeid."' OR tradeid LIKE '%,".$tradeid."' OR tradeid LIKE '%,".$tradeid.",%' OR tradeid LIKE '%".$tradeid.",%' ) ");
		  								  //(tradeid LIKE '".$Trade."' OR tradeid LIKE '%,".$Trade."' OR tradeid LIKE '%,".$Trade.", %' OR tradeid LIKE '".$Trade.",%' )
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}
	
	
	## Get user details by postal code and job type
	function getAllJOBFormpostalcodeTradeIdJobID($jobtypeid='',$postalcode,$tradeid,$jobtypeId,$limit='',$offset='') {
		$fields=array("");	
		$tables=array($this->userjobpost);
		if($tradeid!='')
		{
			$exp_arr_trade = @explode(",",$tradeid);


			for($e=0;$e<count($exp_arr_trade);$e++)
			{
				$new_arr_trade[] = "'".$exp_arr_trade[$e]."'";	
			}	

			if(!empty($new_arr_trade))
			{
			$imp_str_trade = 	@implode(",",$new_arr_trade);
			}
			else
				$imp_str_trade = '0';
		}else{
				$imp_str_trade = '0';
		}
		
		if($jobtypeId!='')
		{
			$exp_arr = @explode(",",$jobtypeId);


			for($e=0;$e<count($exp_arr);$e++)
			{
				$new_arr[] = "'".$exp_arr[$e]."'";	
			}	

			if(!empty($new_arr))
			{
				$imp_str_jobtype = 	@implode(",",$new_arr);
			}
			else
				$imp_str_jobtype = '0';
		}else{
		$imp_str_jobtype = '0';
		}

		$where = array(" status = '1' AND project_status ='Open' AND area ='".$postalcode."' AND trade_id IN (".$imp_str_trade.") and jobtype_id IN (".$imp_str_jobtype.") ");
		
		if($jobtypeid!='')
		{
			$where[] = "(jobtype_id =".$jobtypeid.")";
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("postjob_id DESC"), $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
	
	
	## Add view job id in database 
	function addUserViewJOBByValue($Array) {
		$this->InsertData( $this->userviewjob , $Array );		
		$insertId = mysql_insert_id();
		return $insertId;
	}
	
	## Get view job id details by userid and jobid
 	function getUserViewjobDetailsByUserIdJobId($id,$jobid) {
		$fields=array();	
		$tables=array($this->userviewjob);
		$where=array("userid=".$id." and jobid = ".$jobid);		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	## Get user leads according to the user Id 
  	function leadsAccordingToUserId($userId,$Jobtype='',$Budget='',$Projectstatus='',$limit =0,$offset = 0,$isPage){
		$fields  = array("uj.*,jp.*,uj.status new_status");
		$tables  = array("$this->userviewjob uj,$this->userjobpost jp");
		 if($isPage == true){
			$limit  = $limit;
			$offset = $offset;
		 }
		$where   = array("uj.userid =".$userId." and uj.jobid=jp.postjob_id");
		if($Jobtype!='0' && $Jobtype!='')
		{
			$where[] = "(jp.jobtype_id ='".$Jobtype."')";
		}
		if($Budget!='0' && $Budget!='')
		{
			$where[] = "(jp.approx_budget ='".$Budget."')";			
		}
		if($Projectstatus!='0' && $Projectstatus!='')
		{
			$where[] = "(uj.status ='".$Projectstatus."')";			
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("id DESC"), $group=array(),$limit,$offset,0);
		$result  = $this->FetchAll($result1);
		return $result;		
    }
	
	## Get user leads according to the user Id 
  	function FindJobAccordingToAll($Trade='',$Jobtype='',$postalcode='',$limit =0,$offset = 0,$isPage){
		$fields  = array("jp.*");
		$tables  = array("$this->userjobpost jp,$this->user u");
		 if($isPage == true){
			$limit  = $limit;
			$offset = $offset;
		 }
		$where   = array("project_status = 'Open' and status='1' and jp.user_id=u.id and u.user_status ='1'");		
		
		if($Trade!='0' && $Trade!='')
		{			
			//$where[] = "(jp.trade_id ='".$Trade."')";			
		
			$exp_arr_trade = @explode(",",$Trade);


			for($e=0;$e<count($exp_arr_trade);$e++)
			{
				$new_arr_trade[] = "'".$exp_arr_trade[$e]."'";	
			}	

			if($new_arr_trade[0]!='')
			{
				$imp_str_trade = 	@implode(",",$new_arr_trade);
			}
			else
				$imp_str_trade = '0';
				
				
			$where[] = "(jp.trade_id in (".$imp_str_trade."))";			
		}
		
		if($Jobtype!='0' && $Jobtype!='')
		{
			$where[] = "(jp.jobtype_id ='".$Jobtype."')";
		}
		
		if($postalcode!='0' && $postalcode!='')
		{
			$where[] = "(jp.area ='".$postalcode."')";			
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("postjob_id DESC"), $group=array(),$limit,$offset,0);
		$result  = $this->FetchAll($result1);
		return $result;		
    }
	
	## Get tradesman user according to the user postal code 
  	function FindTradesmanAccordingToAll($Trade='',$postalcode='',$limit =0,$offset = 0,$isPage){
		$fields  = array("u.*");
		$tables  = array("$this->user u");
		 if($isPage == true){
			$limit  = $limit;
			$offset = $offset;
		 }
		
		
		if($Trade!='0' && $Trade!='')
		{
			
			//$where[] = "(tradeid ='".$Trade."')";			
			$str  = (" And (tradeid LIKE '".$Trade."' OR tradeid LIKE '%,".$Trade."' OR tradeid LIKE '%,".$Trade.", %' OR tradeid LIKE '".$Trade.",%' )");
		}		
		
		$where   = array("u.user_status ='1' and u.user_type ='4' $str");		
		
		if($postalcode!='0' && $postalcode!='')
		{
			$where[] = "(postalcode in (".$postalcode."))";			
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("review_count DESC"), $group=array(),$limit,$offset,0);
		$result  = $this->FetchAll($result1);
		return $result;		
    }
	
	
  	// Delete Welcome Menu
	function deleteLeadById($id)
	{
		$this->DeleteData($this->userviewjob,"id",$id);
	}

	## Get view job id details by Id
 	function getUserViewjobDetailsByID($id) {
		$fields=array();	
		$tables=array($this->userviewjob);
		$where=array("id=".$id);
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1); 
		return $result;		
	}

	// Delete transaction id
	function deleteTransactionById($id)
	{
		$this->DeleteData($this->usermembershipdetail,"id",$id);
		$this->DeleteData($this->usermembership,"id",$id);
	}
	
	## Get user payment history according to the user Id 
  	function paymentHistoryAccordingToUserId($userId,$paymentfor='', $fromDate='', $toDate='',$limit =0,$offset = 0,$isPage=""){
		$fields  = array("umd.*");
		$tables  = array("$this->usermembershipdetail umd");
		 if($isPage == true){
			$limit  = $limit;
			$offset = $offset;
		 }		 
		$where   = array("umd.user_id =".$userId."");
		if($paymentfor!='')
			$where[] = "(umd.paymentfor ='".$paymentfor."')";
		
		if($fromDate!='' && $toDate!=''){
			$where[] = "DATE(status_date) BETWEEN '".$fromDate."' AND '".$toDate."' ";
		}		
		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("id DESC"), $group=array(),$limit,$offset,0);
		$result  = $this->FetchAll($result1);
		return $result;		
   }
   
   
	## Get user payment history according to the user Id 
  	function paymentHistoryAccordingToUserIdForMembershipPlan($userId,$paymentfor='', $fromDate='', $toDate='',$limit =0,$offset = 0,$isPage=""){
		$fields  = array("umd.*");
		$tables  = array("$this->usermembershipdetail umd");
		 if($isPage == true){
			$limit  = $limit;
			$offset = $offset;
		 }		 
		$where   = array("umd.user_id =".$userId." and paymentfor='membershipplan'");
		if($paymentfor!='')
			$where[] = "(umd.paymentfor ='".$paymentfor."')";
		
		if($fromDate!='' && $toDate!=''){
			$where[] = "DATE(status_date) BETWEEN '".$fromDate."' AND '".$toDate."' ";
		}		
		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("id DESC"), $group=array(),$limit,$offset,0);
		$result  = $this->FetchAll($result1);
		return $result;		
   }
   
	
	## Get user leads according to the user Id 
  	function JobsAccordingToUserId($userId,$Jobtype='',$Budget='',$Projectstatus='',$limit =0,$offset = 0,$isPage){
		$fields  = array("jp.*");
		$tables  = array("$this->userjobpost jp");
		 if($isPage == true){
			$limit  = $limit;
			$offset = $offset;
		 }
		$where   = array("jp.user_id =".$userId."");
		if($Jobtype!='0' && $Jobtype!='')
		{
			$where[] = "(jp.jobtype_id =".$Jobtype.")";
		}
		if($Budget!='0' && $Budget!='')
		{
			$where[] = "(jp.approx_budget ='".$Budget."')";			
		}
		if($Projectstatus!='0' && $Projectstatus!='')
		{
			$where[] = "(jp.project_status ='".$Projectstatus."')";			
		}
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("postjob_id DESC"), $group=array(),$limit,$offset,0);
		$result  = $this->FetchAll($result1);
		return $result;		
    }
  	// Delete Welcome Menu
	function deleteJobById($id)
	{
		$this->DeleteData($this->userjobpost,"postjob_id",$id);
	}

	## Get tradesman user count details by job Id
 	function getleadcountofTradesmanByJobId($jobid) {
		$fields=array("count(id) cnt");	
		$tables=array($this->userviewjob);
		$where=array("jobid=".$jobid);		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	## Get tradesman user count details by user Id
 	function getleadcountofTradesmanByUserId($userid) {
		$fields=array("count(id) cnt");	
		$tables=array($this->userviewjob);
		$where=array("userid =".$userid);		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}	

	function MYJOBLEADSAccordingToJOBId($jobsID,$limit='',$offset='') {
		$fields=array("$this->userviewjob.*,$this->user.first_name,$this->user.last_name");	
		$tables=array($this->userviewjob,$this->user);		
		$where = array("jobid ='".$jobsID."' and userid = $this->user.id");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
	
	## Edit user view job by id
	function editUserViewJobValueById($array, $Id){
		$this->UpdateData($this->userviewjob,$array,"id",$Id,0);		
	}

	## Get all user details by status open
	function getallusersjobviewStatusOpen($jobsID,$limit='',$offset='') {
		$fields=array("$this->userviewjob.*,$this->user.first_name,$this->user.last_name");	
		$tables=array($this->userviewjob,$this->user);		
		$where = array("jobid ='".$jobsID."' and userid = $this->user.id and $this->userviewjob.status='Open'");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
	
	
	function deleteUserJobViewByJOBId($jobid)
	{
		$this->DeleteData($this->userviewjob,"jobid",$jobid);
	}
	
	
	## Add report abuse in database
	function addReporttoabuse($Array) {
		$this->InsertData( $this->reportabuse , $Array );		
		$insertId = mysql_insert_id();
		return $insertId;
	}	
	## Get all user details by status open
	function getAllReportAbuse($search='',$col1='',$col2='',$limit='',$offset='') {
		$fields=array("$this->reportabuse.*,$this->user.first_name,$this->user.last_name");	
		$tables=array($this->reportabuse,$this->user);		
		$where = array("userid = $this->user.id ");		
		
		if($search != '') {		
			$where[] = "(concat(first_name,' ',last_name) LIKE '%".$search."%' OR comment LIKE '%".$search."%' )";
		}
		if($col1 =='0')
		{
			$order = array("jobid ASC");
		}
		if($col2 =='0')
		{
			$order = array("first_name ASC");
		}
		
		if($col1 =='1')
		{
			$order = array("jobid DESC");
		}
		if($col2 =='1')
		{
			$order = array("first_name DESC");
		}
		
		$result1 = $this->SelectData($fields,$tables, $where, $order, $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
		## Get report abuse detail by id
	function getReportDetailsById($id) {
		$fields=array("");	
		$tables=array($this->reportabuse);
		$where=array("report_id=".$id);		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	
	function deleteReportAbuseById($id){
		$this->DeleteData($this->reportabuse,"report_id",$id);
	}
	
	## Get view jobdetail by jobid and status
 	function getUserViewjobDetailsByJIDANDSTATUS($id,$status='') {
		$fields=array();	
		$tables=array($this->userviewjob);
		$where=array("jobid=".$id);
		if($status!='')
			$where[] = "(status ='".$status."')";			
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1); 
		return $result;		
	}

	function getReviewdetailByFromUIDToUIDJobID($jobid,$fromid,$toid)
	{
		$fields=array();	
		$tables=array($this->reviews);
		$where=array("job_id=".$jobid." and from_id =".$fromid." and to_id=".$toid."");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	function getUserRatingCount($userid)
	{
		$fields=array("sum(rating) ratingsum,count(id) cnt");	
		$tables=array($this->reviews);
		$where=array("to_id=".$userid."");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	function getUserLastReviewDetail($userid)
	{
		$fields=array("$this->reviews.*,$this->user.first_name,$this->user.last_name");	
		$tables=array($this->reviews,$this->user);
		$where=array("$this->reviews.from_id = $this->user.id and to_id=".$userid."");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("id asc"), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	function getUserAllReviewDetail($userid)
	{
		$fields=array("$this->reviews.*,$this->user.first_name,$this->user.last_name");	
		$tables=array($this->reviews,$this->user);
		$where=array("$this->reviews.from_id = $this->user.id and to_id=".$userid."");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("id asc"), $group=array(),$limit = "",0,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}
	
	function getReviewByUserId($userID='',$searchtxt='',$col1='',$limit='',$offset='') {
		$fields=array("$this->reviews.*,$this->user.first_name,$this->user.last_name,$this->userjobpost.job_desc");	
		$tables=array($this->reviews,$this->user,$this->userjobpost);		
		$where = array("$this->reviews.from_id = $this->user.id and $this->reviews.job_id = $this->userjobpost.postjob_id");		
		if($userID!='')
		{
			$where[]= " $this->reviews.to_id ='".$userID."'"	;
		}
		if($searchtxt!='')
		{
			$where[] = "(concat($this->user.first_name,' ',$this->user.last_name) LIKE '%".$searchtxt."%' )";	
		}
		if($col1 =='0')
		{
			$order = array("job_id ASC");
		}
		
		if($col1 =='1')
		{
			$order = array("job_id DESC");
		}
		if($col1 !=='0' && $col1 !='1')
		{
			$order = array("$this->reviews.id DESC");
		}
		
		$result1 = $this->SelectData($fields,$tables, $where, $order, $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
	
	function getTwoDescJobArray() 
	{
		$fields=array("$this->userjobpost.*,$this->user.first_name,$this->user.last_name");	
		$tables=array($this->userjobpost,$this->user);		
		$where = array("user_id = $this->user.id and status='1'");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("postjob_id DESC"), $group=array(), $limit='2',0,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}	
	
	
	function getallReviewUser()
	{		
		
		$sql ="SELECT to_id, round( sum( getbids_job_reviews.rating ) / count( getbids_job_reviews.id ) ) avgrating, getbids_users.first_name, getbids_users.last_name,getbids_users.companyname, getbids_users.tradeid, getbids_users.phone_number, getbids_users.workarea,getbids_users.user_avatar
FROM getbids_job_reviews, getbids_users
WHERE getbids_job_reviews.to_id = getbids_users.id and getbids_users.user_status='1'
GROUP BY to_id
ORDER BY `avgrating` DESC
LIMIT 0 , 2 ";
	
		$result1= $this->ExecuteQuery($sql);	 
		$result= $this->FetchAll($result1); 
	
		return $result;		
	}
	
	## fetch last 5 registered tradesman user
	function getLasrFiveRegisteredTradesman() {
		$fields=array();	
		$tables=array($this->user);
		$where=array("user_type='4'");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array("registered_date DESC"), $group=array(),$limit = "5",0,0); 
		$result= $this->FetchAll($result1); 
		return $result;	
	}
	
	## fetch last 5 registered homeowner user
	function getLasrFiveRegisteredHomeowner(){
		$fields = array();
		$tables = array($this->user);
		$where = array("user_type='3'");
		$result1 = $this->SelectData($fields,$tables,$where,$order=array("registered_date DESC"),$group= array(),$limit="5",0,0);
		$result = $this->FetchAll($result1);
		return $result;
	}
	

	
	## fetch last 5 posted job
	function getLasrPostedJob(){
		$fields=array("$this->userjobpost.*,$this->trade_cat.trade_category");	
		$tables=array($this->userjobpost,$this->trade_cat);
		$where = array("$this->userjobpost.trade_id = $this->trade_cat.id");
		$result1 = $this->SelectData($fields,$tables,$where,$order=array("$this->userjobpost.postjob_id DESC"),$group=array(),$limit="5",0,0);
		$result = $this->FetchAll($result1);
		return $result;
	}
	
	function gettopthreeTradesjobs(){
		$sql ="SELECT count( getbids_postjob.trade_id ) as cnt, getbids_postjob.trade_id, getbids_trade_category.trade_category
FROM getbids_postjob, getbids_trade_category
WHERE getbids_postjob.trade_id = getbids_trade_category.id
GROUP BY getbids_postjob.trade_id
ORDER BY `cnt` DESC
LIMIT 0 , 3 ";

		$result1= $this->ExecuteQuery($sql);	 
		$result= $this->FetchAll($result1); 
	
		return $result;		
	}
	
	function getrelatedTradesjobs($postcode){
		
		$fields=array("$this->userjobpost.*,$this->trade_cat.trade_category");	
		$tables=array($this->userjobpost,$this->trade_cat);
		if($postcode)
			$where = array("$this->userjobpost.trade_id = $this->trade_cat.id and $this->userjobpost.area in ($postcode)");
		else
			$where = array("$this->userjobpost.trade_id = $this->trade_cat.id ");
		
		$result1 = $this->SelectData($fields,$tables,$where,$order=array(""),$group=array("trade_id"),$limit="",0,0);
		$result = $this->FetchAll($result1);
		return $result;
	
	}
	
	## Get user Work Photo count details by user id
	function getUserPhotoCntDetailsByUserId($id) {
		$fields=array("count(photo_id) as cnt");	
		$tables=array($this->user_workphoto);
		$where=array("user_id=".$id);		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	
	## Get post job detail by id
	function getJobDetailsWithOpen() 
	{
		$fields=array("$this->userjobpost.*");	
		$tables=array($this->userjobpost);
		$where=array("status ='1' and project_status ='Open'");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchAll($result1); 
		return $result;		
	}
	function getJobviewDetailsWithAccept($jobid='') 
	{
		$fields=array("$this->userviewjob.*");	
		$tables=array($this->userviewjob);
		$where=array("status ='Accept'");		
		if($jobid!='')
			$where[]= " $this->userviewjob.jobid ='".$jobid."'"	;
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchAll($result1); 
		return $result;		
	}
	
	function getReviewdetailByFromToUIDAndJobID($jobid,$toid)
	{
		$fields=array();	
		$tables=array($this->reviews);
		$where=array("job_id=".$jobid." and to_id=".$toid."");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0);
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	
	function getJobDetailsByIdWithAccept($id) {
		$fields=array("$this->user.first_name,$this->user.last_name,$this->user.phone_number,$this->user.email,$this->userjobpost.*,$this->trade_cat.trade_category");	
		$tables=array($this->user,$this->userjobpost,$this->trade_cat);
		$where=array("$this->user.id = $this->userjobpost.user_id and  $this->userjobpost.trade_id = $this->trade_cat.id and postjob_id=".$id." and $this->userjobpost.project_status ='Accept'");		
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(),$limit = "",0,0); 
		$result= $this->FetchRow($result1); 
		return $result;		
	}
	## Add user in database
	function addWorkAreaByValue($Array) {
		$this->InsertData( $this->workarea , $Array );		
		$insertId = mysql_insert_id();
		return $insertId;
	}	
	## Edit user by userid
	function editWorkAreaValueById($array, $Id){
		$this->UpdateData($this->workarea,$array,"id",$Id,0);
		
	}
	## Delete user by userid
	function deleteWorkAreaValueByUserId($id){
		$this->DeleteData($this->workarea,"user_id",$id);
	}
	
		## Get user all long lat details by userid
	function getAllLongLatByUserID($id) {
		$fields=array();	
		$tables=array($this->workarea);
		$where = array("user_id=".$id."");
		$result1 = $this->SelectData($fields,$tables, $where, $order = array(), $group=array(), $limit,$offset,0);
		$result= $this->FetchAll($result1); 
		return $result;		
	}

	## Delete postjob with multiple jobids
	function deleteMultiplereportabuse($ids) {
		$sql = "DELETE FROM ".$this->reportabuse." WHERE report_id IN (".$ids.")";
		$result1= $this->ExecuteQuery($sql);	 
	}


	
}



?>
